package com.ihg.hiex.entity.session.before.finaltest;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.ihg.hiex.entity.session.Session;
import com.ihg.hiex.entity.user.Assessor;

/**
 * 观察员认证考试结果
 *
 * @author Ark
 *
 */
@Entity
@Table(name = "final_test")
public class FinalTest {
    
    @Id
    @GeneratedValue
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    private Session session;
    
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    private Assessor assessor;
    
    /** 基础版答题成绩 */
    private int examOneResult;
    
    /** 基础版题目总数 */
    private int examOneTotals;

    /** 基础版是否完成 */
    @Column(name = "is_finish_one")
    private boolean finishOne;
    
    /** 巩固版答题成绩 */
    private int examTwoResult;
    
    /** 巩固版题目总数 */
    private int examTwoTotals;
    
    /** 巩固版是否完成 */
    @Column(name = "is_finish_two")
    private boolean finishTwo;
    
    /** 是否通过测试 */
    @Column(name = "is_pass")
    private boolean pass; 
    
    /** 最终提交时间 */
    private Date submitTime;
    
    /** 是否已看指导视频 */
    @Column(name = "is_visit_vedio")
    private boolean visitVedio;

    @Column(name = "is_disabled")
    private boolean disabled;
    
    /** 基础版练习次数 */
    @Transient
    private int basicExerciseCount;
    
    /** 巩固版练习次数 */
    @Transient
    private int enhanceExerciseCount;
    
    /**
     * The default constructor
     */
    public FinalTest() {
    }
    
    /**
     * @param session
     * @param assessor
     * @param examOneTotals
     * @param examTwoTotals
     */
    public FinalTest(Session session, Assessor assessor, int examOneTotals, int examTwoTotals) {
        this.session = session;
        this.assessor = assessor;
        this.examOneTotals = examOneTotals;
        this.examTwoTotals = examTwoTotals;
        this.examOneResult = 0;
        this.examTwoResult = 0;
        this.pass = false;
        this.finishOne = false;
        this.finishTwo = false;
        this.visitVedio = false;
        this.disabled = false;
    }
    
    /**
     * 
     * @param assessor
     * @param examOneTotals
     * @param examTwoTotals
     */
    public FinalTest(Assessor assessor, int examOneTotals, int examTwoTotals) {
        this.assessor = assessor;
        this.examOneTotals = examOneTotals;
        this.examTwoTotals = examTwoTotals;
        this.examOneResult = 0;
        this.examTwoResult = 0;
        this.pass = false;
        this.finishOne = false;
        this.finishTwo = false;
        this.visitVedio = false;
        this.disabled = false;
    }

    /**
     * 
     * @param examOneResult
     * @return
     */
    public FinalTest editOne(int examOneResult) {
        this.examOneResult = examOneResult;
        return this;
    }
    
    /**
     * @param examTwoResult
     * @return
     */
    public FinalTest editTwo(int examTwoResult) {
        this.examTwoResult = examTwoResult;
        return this;
    }
    
    /**
     * 
     * @param examOneResult
     * @return
     */
    public FinalTest finishOne(int examOneResult) {
        this.examOneResult = examOneResult;
        this.finishOne = true;
        this.submitTime = new Date();
        return this;
    }
    
    /**
     * @param examTwoResult
     * @return
     */
    public FinalTest finishTwo(int examTwoResult) {
        this.examTwoResult = examTwoResult;
        this.finishTwo = true;
        this.pass = true;
        this.submitTime = new Date();
        return this;
    }
    
    /**
     * @return
     */
    @Transient
    public boolean isPassOne() {
        
        BigDecimal examOneResultValue = new BigDecimal(examOneResult);
        BigDecimal examOneTotalsValue = new BigDecimal(examOneTotals);
        
        BigDecimal correctRate = examOneResultValue.divide(examOneTotalsValue, 2, RoundingMode.HALF_UP);
        
        if (correctRate.compareTo(new BigDecimal("0.7")) != -1) {
            return true;
        }
        
        return false;
        
    }
    
    /**
     * @return
     */
    @Transient
    public boolean isPassTwo() {
        
        BigDecimal examTwoResultValue = new BigDecimal(examTwoResult);
        BigDecimal examTwoTotalsValue = new BigDecimal(examTwoTotals);
        
        BigDecimal correctRate = examTwoResultValue.divide(examTwoTotalsValue, 2, RoundingMode.HALF_UP);
        
        if (correctRate.compareTo(new BigDecimal("0.7")) != -1) {
            return true;
        }
        
        return false;
        
    }
    
    /**
     * get answer correct rate
     * 
     * @return
     */
    @Transient
    public BigDecimal getCorrectRateOne() {
        BigDecimal correctCountValue = new BigDecimal(examOneResult);
        BigDecimal scoreCountValue = new BigDecimal(examOneTotals);
        return correctCountValue.divide(scoreCountValue, 2, RoundingMode.HALF_UP);
    }
    
    /**
     * get answer correct rate
     * 
     * @return
     */
    @Transient
    public BigDecimal getCorrectRateTwo() {
        BigDecimal correctCountValue = new BigDecimal(examTwoResult);
        BigDecimal scoreCountValue = new BigDecimal(examTwoTotals);
        return correctCountValue.divide(scoreCountValue, 2, RoundingMode.HALF_UP);
    }
    
    /**
     * @return
     */
    public FinalTest visitVedio() {
        this.visitVedio = true;
        return this;
    }
    
    /**
     * @return
     */
    public FinalTest pass() {
        this.pass = true;
        return this;
    }
    
    /**
     * 
     * @return
     */
    public FinalTest rePass() {
        this.pass = false;
        return this;
    }
    
    /**
     * @param basicExerciseCount
     * @param enhanceExerciseCount
     * @return
     */
    public FinalTest reExerciseCount(int basicExerciseCount, int enhanceExerciseCount) {
        this.basicExerciseCount = basicExerciseCount;
        this.enhanceExerciseCount = enhanceExerciseCount;
        return this;
    }
    
    /**
     * 
     * @return
     */
    public boolean isShowCertificate() {
        return this.finishOne && this.getCorrectRateOne().compareTo(new BigDecimal(0.7)) >= 0;
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the session
     */
    public Session getSession() {
        return session;
    }

    /**
     * @return the assessor
     */
    public Assessor getAssessor() {
        return assessor;
    }

    /**
     * @return the examOneResult
     */
    public int getExamOneResult() {
        return examOneResult;
    }

    /**
     * @return the examOneTotals
     */
    public int getExamOneTotals() {
        return examOneTotals;
    }

    /**
     * @return the finishOne
     */
    public boolean isFinishOne() {
        return finishOne;
    }

    /**
     * @return the examTwoResult
     */
    public int getExamTwoResult() {
        return examTwoResult;
    }

    /**
     * @return the examTwoTotals
     */
    public int getExamTwoTotals() {
        return examTwoTotals;
    }

    /**
     * @return the finishTwo
     */
    public boolean isFinishTwo() {
        return finishTwo;
    }
    
    /**
     * @return
     */
    public boolean isPass() {
        return pass;
    }

    /**
     * @return the submitTime
     */
    public Date getSubmitTime() {
        return submitTime;
    }
    
    /**
     * @return
     */
    public boolean isVisitVedio() {
        return visitVedio;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }
    
    /**
     * @return
     */
    public int getBasicExerciseCount() {
        return basicExerciseCount;
    }
    
    /**
     * @return
     */
    public int getEnhanceExerciseCount() {
        return enhanceExerciseCount;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param session the session to set
     */
    public void setSession(Session session) {
        this.session = session;
    }

    /**
     * @param assessor the assessor to set
     */
    public void setAssessor(Assessor assessor) {
        this.assessor = assessor;
    }

    /**
     * @param examOneResult the examOneResult to set
     */
    public void setExamOneResult(int examOneResult) {
        this.examOneResult = examOneResult;
    }

    /**
     * @param examOneTotals the examOneTotals to set
     */
    public void setExamOneTotals(int examOneTotals) {
        this.examOneTotals = examOneTotals;
    }

    /**
     * @param finishOne the finishOne to set
     */
    public void setFinishOne(boolean finishOne) {
        this.finishOne = finishOne;
    }

    /**
     * @param examTwoResult the examTwoResult to set
     */
    public void setExamTwoResult(int examTwoResult) {
        this.examTwoResult = examTwoResult;
    }

    /**
     * @param examTwoTotals the examTwoTotals to set
     */
    public void setExamTwoTotals(int examTwoTotals) {
        this.examTwoTotals = examTwoTotals;
    }

    /**
     * @param finishTwo the finishTwo to set
     */
    public void setFinishTwo(boolean finishTwo) {
        this.finishTwo = finishTwo;
    }
    
    /**
     * @param pass
     */
    public void setPass(boolean pass) {
        this.pass = pass;
    }

    /**
     * @param submitTime the submitTime to set
     */
    public void setSubmitTime(Date submitTime) {
        this.submitTime = submitTime;
    }
    
    /**
     * @param visitVedio
     */
    public void setVisitVedio(boolean visitVedio) {
        this.visitVedio = visitVedio;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }
    
    /**
     * @param basicExerciseCount
     */
    public void setBasicExerciseCount(int basicExerciseCount) {
        this.basicExerciseCount = basicExerciseCount;
    }
    
    /**
     * @param enhanceExerciseCount
     */
    public void setEnhanceExerciseCount(int enhanceExerciseCount) {
        this.enhanceExerciseCount = enhanceExerciseCount;
    }
    
}